/*
 * @(#)Main.java
 *
 * This work is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This work is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * Copyright (c) 2003 Per Cederberg. All rights reserved.
 */
package tetris;

import genetics.Grid;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * The main class of the Tetris game. This class contains the necessary methods
 * to run the game either as a stand-alone application or as an applet inside a
 * web page.
 *
 * @version 1.2
 * @author Per Cederberg, per@percederberg.net
 */
public class Main extends Applet
{

    /**
     * The applet parameter information structure.
     */
    private static final String PARAMETER[][] = {
        {"tetris.color.background", "color",
            "The overall background color."},
        {"tetris.color.label", "color",
            "The text color of the labels."},
        {"tetris.color.button", "color",
            "The start and pause button bolor."},
        {"tetris.color.board.background", "color",
            "The background game board color."},
        {"tetris.color.board.message", "color",
            "The game board message color."},
        {"tetris.color.figure.square", "color",
            "The color of the square figure."},
        {"tetris.color.figure.line", "color",
            "The color of the line figure."},
        {"tetris.color.figure.s", "color",
            "The color of the 's' curved figure."},
        {"tetris.color.figure.z", "color",
            "The color of the 'z' curved figure."},
        {"tetris.color.figure.right", "color",
            "The color of the right angle figure."},
        {"tetris.color.figure.left", "color",
            "The color of the left angle figure."},
        {"tetris.color.figure.triangle", "color",
            "The color of the triangle figure."}
    };
    /**
     * The Tetris game being played (in applet mode).
     */
    private Game game = null;
    private static boolean enableGUI = true;
    private static boolean randomizeSeed = false;

    /**
     * The stand-alone main routine.
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) throws AWTException
    {
        Game game = new Game(enableGUI);
        game.setRandomizeSeed(randomizeSeed);
        Grid grid = new Grid(game);

        if (enableGUI) {
            Frame frame = new Frame("Tetris");
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Dimension dim = toolkit.getScreenSize();
            frame.setLocation((dim.width / 2) - (346 / 2), (dim.height / 2) - (438 / 2));

            // Set up frame
            frame.add(game.getComponent());
            frame.pack();

            // Add frame window listener
            frame.addWindowListener(new WindowAdapter()
            {

                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });

            // Show frame (and start game)
            frame.show();
        } else {
            game.getComponent();
        }

        //Vanlig körning 
        //game.setSeed(0); // behövs sättas om man inte kör randomseed
//        grid.populateGrid();
//        while (true) {
//            grid.games();
//        }
        //Slut vanlig körning

        // Play a game with a single disk and seed.
        double[] disc = new double[11];

//        // Seed 0 disc, 3.9m score
//        disc[0] = 4.385084156060504;
//        disc[1] = 1.4152871400132994;
//        disc[2] = 92.6541743165992;
//        disc[3] = 0.4951094712262627;
//        disc[4] = 98.63792693034704;
//        disc[5] = 66.98936424074219;
//        disc[6] = 92.50678856992042;
//        disc[7] = 18.40458971927974;
//        disc[8] = 11.749380908608376;
//        disc[9] = 73.76422526893136;
//        disc[10] = 12.839053180530913;

        // Random seed disc, 2.7m score
        disc[0] = 14.681673111120407;
        disc[1] = 7.232572763921485;
        disc[2] = 79.5846162982927;
        disc[3] = 0.05945764128797437;
        disc[4] = 80.71875498711924;
        disc[5] = 60.550335695088556;
        disc[6] = 84.82194308291207;
        disc[7] = 96.15136490310084;
        disc[8] = 14.4497525040454;
        disc[9] = 96.64177613246898;
        disc[10] = 13.308987941493378;


        game.setSeed(2193872); // Must do this
        System.out.println(grid.playSingleGame(disc)); // Must do this
        //SLut single game
    }

    /**
     * Returns information about the parameters that are understood by this
     * applet.
     *
     * @return an array describing the parameters to this applet
     */
    public String[][] getParameterInfo()
    {
        return PARAMETER;
    }

    /**
     * Initializes the game in applet mode.
     */
    public void init()
    {
        String value;

        // Set all configuration parameters
        for (int i = 0; i < PARAMETER.length; i++) {
            value = getParameter(PARAMETER[i][0]);
            if (value != null) {
                Configuration.setValue(PARAMETER[i][0], value);
            }
        }

        // Create game object
        game = new Game(enableGUI);

        // Initialize applet component
        setLayout(new BorderLayout());
        add(game.getComponent(), "Center");
    }

    /**
     * Stops the game in applet mode.
     */
    public void stop()
    {
        game.quit();
    }

    /**
     * A dummy COM object wrapper. This class has been created only to avoid the
     * erroneous HTTP lookup for it when the Tetris game is run as an applet in
     * some browsers.
     *
     * @version 1.0
     * @author Per Cederberg, per@percederberg.net
     */
    public static class COMClassObject extends Object
    {
    }
}
